package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.${className};
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xphsc.easyjdbc.core.metadata.ElementResolver;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

@Repository
@Slf4j
public class ${className}Mapper extends BaseMapper {
    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public PageInfo<${className}> findByFastParam(FastParam fastParam) {
        String tableName = ElementResolver.resolve(${className}.class).getTable();
        PageInfo<${className}> pageInfo = this.easyJdbcTemplate.findByPage(fastParam.getSql(tableName), ${className}.class, fastParam.getOffset(), fastParam.getLimit());
        return pageInfo;
    }

    public ${className} findById(Long ids) {
        ${className} ${classname} = this.easyJdbcTemplate.getByPrimaryKey(${className}.class, ids);
        return ${classname};
    }

    public void deleteById(String id) {
        this.easyJdbcTemplate.deleteByPrimaryKey(${className}.class, id);
    }

    public void update(${className} ${classname}) {
        ${classname}.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        this.easyJdbcTemplate.update(${classname});
    }

    public Object save(${className} ${classname}) {
        ${classname}.setCreate_time(new Timestamp(System.currentTimeMillis()));
        ${classname}.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(${classname});

    }

    public List<${className}> findByIds(String ids) {
        if (StrUtil.isBlank(ids)) {
            return CollUtil.newArrayList();
        }
        return this.easyJdbcTemplate.findByIds(${className}.class, Arrays.asList(StrUtil.splitToArray(ids, ',')));

    }
}
